package editor.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import editor.view.mousehandling.MousePointer;

import map.MapObject;
import map.RasterCell;
import map.blocks.DefaultMapObject;
import map.blocks.InterstateTeleporter;
import map.blocks.MultipleCellBlock;
import map.blocks.SolidBlock;
import map.blocks.Teleporter;
import map.terrain.BasicTerrain;
import map.terrain.DefaultTerrain;
import map.terrain.SolidTerrain;

public class SubPanel extends JPanel {

	private static final int BUTTON_BORDER_SIZE = 2;
	
	MapEditorPanel map;
	ArrayList<JButton> buttons;
	
	public SubPanel() {
		setPreferredSize(new Dimension(100,60));
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		buttons = new ArrayList<JButton>();
		
		JPanel blockPanel = new JPanel();
		blockPanel.setSize(new Dimension(30, 100));
		blockPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		blockPanel.setBorder(BorderFactory.createTitledBorder("Block Tools"));
		
		//Block-tools
		blockPanel.add(createObjectButton(new SolidBlock(Color.green), "green.png"));
		blockPanel.add(createObjectButton(new SolidBlock(Color.white), null));
		blockPanel.add(createObjectButton(new Teleporter(), null));
		blockPanel.add(createObjectButton(new InterstateTeleporter(), null));
		
		MapObject[][] a = new MapObject[5][5];
		a[0][0] = new SolidBlock(Color.YELLOW);
		a[1][0] = new SolidBlock(Color.YELLOW);
		a[2][0] = new SolidBlock(Color.YELLOW);
		a[3][0] = new SolidBlock(Color.YELLOW);
		a[4][0] = new SolidBlock(Color.YELLOW);
		
		a[0][1] = new SolidBlock(Color.PINK);
		a[1][1] = new SolidBlock(Color.PINK);
		a[2][1] = new SolidBlock(Color.PINK);
		a[3][1] = new SolidBlock(Color.PINK);
		a[4][1] = new SolidBlock(Color.PINK);
		
		a[0][2] = new SolidBlock(Color.RED);
		a[1][2] = new SolidBlock(Color.RED);
		a[2][2] = new SolidBlock(Color.RED);
		a[3][2] = new SolidBlock(Color.RED);
		a[4][2] = new SolidBlock(Color.RED);
		
		a[0][3] = new SolidBlock(Color.GREEN);
		a[1][3] = new SolidBlock(Color.GREEN);
		a[2][3] = new SolidBlock(Color.GREEN);
		a[3][3] = new SolidBlock(Color.GREEN);
		a[4][3] = new SolidBlock(Color.GREEN);
		
		a[0][4] = new SolidBlock(Color.BLUE);
		a[1][4] = new SolidBlock(Color.BLUE);
		a[2][4] = new SolidBlock(Color.BLUE);
		a[3][4] = new SolidBlock(Color.BLUE);
		a[4][4] = new SolidBlock(Color.BLUE);
		
		MapObject[][] b = new MapObject[5][5];
		b[0][0] = new SolidBlock(Color.YELLOW);
		b[0][1] = new SolidBlock(Color.YELLOW);
		b[0][2] = new SolidBlock(Color.YELLOW);
		b[0][3] = new SolidBlock(Color.YELLOW);
		b[0][4] = new SolidBlock(Color.YELLOW);
		
		b[1][0] = new SolidBlock(Color.PINK);
		b[1][1] = new SolidBlock(Color.PINK);
		b[1][2] = new SolidBlock(Color.PINK);
		b[1][3] = new SolidBlock(Color.PINK);
		b[1][4] = new SolidBlock(Color.PINK);
		
		b[2][0] = new SolidBlock(Color.RED);
		b[2][1] = new SolidBlock(Color.RED);
		b[2][2] = new SolidBlock(Color.RED);
		b[2][3] = new SolidBlock(Color.RED);
		b[2][4] = new SolidBlock(Color.RED);
		
		b[3][0] = new SolidBlock(Color.GREEN);
		b[3][1] = new SolidBlock(Color.GREEN);
		b[3][2] = new SolidBlock(Color.GREEN);
		b[3][3] = new SolidBlock(Color.GREEN);
		b[3][4] = new SolidBlock(Color.GREEN);
		
		b[4][0] = new SolidBlock(Color.BLUE);
		b[4][1] = new SolidBlock(Color.BLUE);
		b[4][2] = new SolidBlock(Color.BLUE);
		b[4][3] = new SolidBlock(Color.BLUE);
		b[4][4] = new SolidBlock(Color.BLUE);
			
		blockPanel.add(createObjectButton(new MultipleCellBlock(5, 5, a), null));
		blockPanel.add(createObjectButton(new MultipleCellBlock(5, 5, b), null));
		
		blockPanel.add(createObjectButton(new DefaultMapObject(), "eraser.png"));
		
		JPanel terrainPanel = new JPanel();
		terrainPanel.setSize(new Dimension(30, 100));
		terrainPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		terrainPanel.setBorder(BorderFactory.createTitledBorder("Terrain"));
		
		//terrain-tools
		terrainPanel.add(createTerrainButton(new SolidTerrain(Color.RED), "default.png"));
		terrainPanel.add(createTerrainButton(new SolidTerrain(Color.GREEN), "green.png"));
		terrainPanel.add(createTerrainButton(new BasicTerrain("dirt.png"), "dirt.png"));
		terrainPanel.add(createTerrainButton(new BasicTerrain("dirt3d.png"), "dirt.png"));
		
		terrainPanel.add(createTerrainButton(new DefaultTerrain(), "eraser.png"));
		
		add(blockPanel);
		add(terrainPanel);
		
	}
	
		
	public MapEditorPanel getMap() {
		return map;
	}



	public void setMap(MapEditorPanel map) {
		this.map = map;
	}

	private JButton createObjectButton(final MapObject object, String image){
		if(image != null){
			if(image.equals("")){
				image = "default.png";
			}
		} else {
			image = "default.png";
		}
			
		final JButton output = new JButton();
		output.setToolTipText(object.toString());
		output.setSize(new Dimension(15,15));
		output.setIcon(new ImageIcon(image));
		output.setBorder(BorderFactory.createLineBorder(Color.BLACK,BUTTON_BORDER_SIZE));
		output.addActionListener(new ActionListener() {
			boolean toggled = false;
				@Override
			public void actionPerformed(ActionEvent arg0) {					
				if(map != null){
					if(!toggled){
						for (JButton btn : buttons) {
							if(btn != arg0.getSource()){
								btn.setEnabled(false);
								btn.setBorderPainted(false);
							}
						}
						output.setBorder(BorderFactory.createLineBorder(Color.CYAN.darker(),BUTTON_BORDER_SIZE));
						map.setCurrPointer(new MousePointer() {

							@Override
							public boolean onClick(RasterCell cell, MouseEvent event) {
								try {
									MapObject newObject = object.newInstance();
									newObject.setCurrentX(cell.getCx());
									newObject.setCurrentY(cell.getCy());
									cell.setAttachedObject(newObject);
								} catch (Exception e) {
									e.printStackTrace();
								}

								return false;
							}
						});
						toggled = true;
					} else {
						output.setBorder(BorderFactory.createLineBorder(Color.BLACK,BUTTON_BORDER_SIZE));
						map.setCurrPointer(null);
						toggled = false;
						for (JButton btn : buttons) {
							btn.setEnabled(true);
							btn.setBorderPainted(true);
						}
					}
				}
			}
		});
		buttons.add(output);
		return output;
	}
	
	//TODO: redundanten code entfernen
	private JButton createTerrainButton(final MapObject object, String image){
		if(image != null){
			if(image.equals("")){
				image = "default.png";
			}
		} else {
			image = "default.png";
		}
			
		final JButton output = new JButton();
		output.setToolTipText(object.toString());
		output.setSize(new Dimension(15,15));
		output.setIcon(new ImageIcon(image));
		output.setBorder(BorderFactory.createLineBorder(Color.BLACK,BUTTON_BORDER_SIZE));
		output.addActionListener(new ActionListener() {
			boolean toggled = false;
				@Override
			public void actionPerformed(ActionEvent arg0) {					
				if(map != null){
					if(!toggled){
						for (JButton btn : buttons) {
							if(btn != arg0.getSource()){
								btn.setEnabled(false);
								btn.setBorderPainted(false);
							}
						}
						output.setBorder(BorderFactory.createLineBorder(Color.CYAN.darker(),BUTTON_BORDER_SIZE));
						map.setCurrPointer(new MousePointer() {

							@Override
							public boolean onClick(RasterCell cell, MouseEvent event) {
								object.setCurrentX(cell.getCx());
								object.setCurrentY(cell.getCy());
								cell.setTerrain(object);
								return false;
							}

						});
						toggled = true;
					} else {
						output.setBorder(BorderFactory.createLineBorder(Color.BLACK,BUTTON_BORDER_SIZE));
						map.setCurrPointer(null);
						toggled = false;
						for (JButton btn : buttons) {
							btn.setEnabled(true);
							btn.setBorderPainted(true);
						}
					}
				}
			}
		});
		buttons.add(output);
		return output;
	}
}


